package com.irssproject.app.web.action.lectorRSS;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.irssproject.app.entitats.Label;
import com.irssproject.app.entitats.Feed;
import com.irssproject.app.entitats.Subscription;
import com.irssproject.app.entitats.User;
import com.irssproject.app.service.lectorRSS.iface.SubscriptionsService;
import com.irssproject.app.utils.Constants;
import com.irssproject.app.web.action.JSONWrappers.FeedJSON;
import com.irssproject.app.web.action.JSONWrappers.SubscriptionJSON;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AutocompleteListsAction extends ActionSupport {
	
	private static final long serialVersionUID = 1L;

	private List<String> etiquetes;
	
	private List<SubscriptionJSON> liSubscripcions;
	
	private List<FeedJSON> llistaFeeds;
	
	private Map<String, Object> session;

	SubscriptionsService subscripcioService;
	
//    private String term;

//    public void setTerm(String term) {
//        this.term = term;
//    }	
	public AutocompleteListsAction(SubscriptionsService subscripcioService) {
		this.subscripcioService = subscripcioService;
	}
	public void setEtiquetes(List<String> etiquetes) {
		this.etiquetes = etiquetes;
	}

	public List<String> getEtiquetes() {
		return etiquetes;
	}

	public void setLiSubscripcions(List<SubscriptionJSON> liSubscripcions) {
		this.liSubscripcions = liSubscripcions;
	}

	public List<SubscriptionJSON> getLiSubscripcions() {
		return liSubscripcions;
	}
	
	public List<FeedJSON> getLlistaFeeds() {
		return llistaFeeds;
	}
	public void setLlistaFeeds(List<FeedJSON> llistaFeeds) {
		this.llistaFeeds = llistaFeeds;
	}
	public void setSession(Map<String, Object> arg0) {
		session = arg0;
	}
	
	public Map<String,Object> getSession(){
		return session;
	}
	

	public String listEtiquetesAutocomplete()
	{	
		List<Label> le = subscripcioService.getLabelsList();
		
		etiquetes = new ArrayList<String>();
		for(Label e:le) {
			etiquetes.add(e.getNom());
		}
		return SUCCESS;
	}
	
	public String listSubscripcionsAutocomplete(){
		
		session = ActionContext.getContext().getSession();
		User usuari = ((User)session.get(Constants.SESSIO_USUARI_AUTENTICAT));

		List<Subscription> llistaSubscripcions = subscripcioService.getSubscriptionsList(usuari,false);
		session.put(Constants.SESSIO_LLISTA_SUBSCRIPCIONS, llistaSubscripcions);

		List<SubscriptionJSON> liJSON = new ArrayList<SubscriptionJSON>();
		
		SubscriptionJSON sJSON;
		
		for (Subscription subs:llistaSubscripcions){

			sJSON = new SubscriptionJSON();
			sJSON.setId(subs.getId());
			sJSON.setFeed(new FeedJSON(subs.getFeed()));
			liJSON.add(sJSON);
		}
		
		setLiSubscripcions(liJSON);
		
		return SUCCESS;
	}
	
	public String listFeedsAutocomplete() throws Exception{
        	llistaFeeds = new ArrayList<FeedJSON>();
        	List<Feed> liFeeds = subscripcioService.getFeedsList();
        	for (Feed f:liFeeds){
        		FeedJSON s = new FeedJSON(f.getTitol(), f.getLink());
        		llistaFeeds.add(s);
        	}
    	return SUCCESS;
	}
	

}
